﻿function GetFormAttachement() {
    var arr = new Array();
    $("#tbFormAttachment input").each(function () {
        var obj = new Object();
        var id = $(this).val();
        var name = $(this).attr("dataName");
        var Attachment = $(this).attr("dataAttachment");
        var remark = $(this).attr("dataRemark");
        obj.ID = id;
        obj.Name = name;
        obj.Attachment = Attachment;
        obj.Remark = remark;
        arr.push(obj);
    });
    var str = JSON.stringify(arr);
    return str;
}




function winClose(isRefresh) {
    if (window.opener != undefined&&isRefresh==1) {
        window.opener.onRefresh();
    }  
    window.close();
}
function Close() {
    winClose(0);
}
function GetFormCondition() {
    var FormCondition = $("#hFormCondition").val();
    if (FormCondition != null && FormCondition !="") {
        var arrCondition = JSON.parse(FormCondition);
        $.each(arrCondition, function (i, item) {
            item.Value = $("#" + item.Value).val();
        });
        return arrCondition;
    }
    return new Array();
} 
function GetProcessCheckUser(ActivityGuid ,ProcessCheckState,callbackFun) {
    var arrCondition = GetFormCondition();
    var ProcessGuid = $("#hProcessGuid").val();
    var AppInstanceID = $("#hAppInstanceID").val();
    var url = '../ProcessEmployee/ProcessItemSelect' + "?ProcessGuid=" + ProcessGuid + "&AppInstanceID=" + AppInstanceID + "&FormCondition=" + JSON.stringify(arrCondition);
    if (ActivityGuid != undefined && ActivityGuid != null) {
        url = url + "&ActivityGuid=" + ActivityGuid;
    }
    if (ProcessCheckState != undefined && ProcessCheckState != null) {
        url = url + "&ProcessCheckState="+ProcessCheckState;
    }
    OpenSelectUserPop(url, callbackFun); 
}
String.prototype.replaceAll = function (reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith);
    } else {
        return this.replace(reallyDo, replaceWith);
    }
}
function GetFormsRelation() {
    var arr = new Array();
    $("#tbFormsRelation input").each(function () {
        var obj = new Object();
        var AppInstanceID = $(this).attr("AppInstanceID");
        var ProcessGUID = $(this).attr("ProcessGUID");
        var AppInstanceFormTitle = $(this).attr("AppInstanceFormTitle");
        obj.AppInstanceFormTitle = AppInstanceFormTitle;
        obj.RelationAppInstanceID = AppInstanceID;
        obj.RelationProcessGUID = ProcessGUID;
        arr.push(obj);
    });
    var str = JSON.stringify(arr);
    return str;
}

function ProcessSendBack() {
    if (confirm("你是否要撤回该流程！")) { 
        var AppName = $("#hAppInstanceName").val();
        var ProcessGUID = $("#hProcessGuid").val();
        var AppInstanceID = $("#hAppInstanceID").val();  
        var url = '../ProcessInstance/ProcessSendBack';
        $.post(url, { AppInstanceID: AppInstanceID, AppName: AppName, ProcessGUID: ProcessGUID }, function (data) {
            if (data.Status != 1) {
                alert(data.Message);
            } else {
                alert("流程撤单成功！");
                winClose(1);
            }
        });
    }
}

function ProcessCopy()
{
    if (confirm("你是否要复制该流程的基础数据?")) {
        var AppName = $("#hAppInstanceName").val();
        var ProcessGUID = $("#hProcessGuid").val();
        var FormType = $("#hFormType").val();
        var AppInstanceID = $("#hAppInstanceID").val();
        var url = '../Form/' + FormType + '?CopyID=' + AppInstanceID + '&&ProcessGuid=' + ProcessGUID;
        var width = screen.availWidth * 0.8;
        var top = screen.availWidth * 0.05;
        var left = screen.availWidth * 0.1;
        var height = screen.availHeight * 0.8;
        window.open(url, "", "width=" + width + "px, height=" + height + "px, top=" + top + "px, left=" + left + "px");
       
    }
}
function call(functionName) {
    eval( functionName + "()");
}
function submitCheckLog() {
    if ($("#hAutoActivity").val() == "True") {
        if (confirm("你是否要结束该流程！")) {
            var options = {
                data: { ProcessConditions: "", CheckLogUser: "[]" },
                success: function (data) { 
                    alert("结束流程成功！");
                    winClose(1);
                }
            };
            $("#checkfrm").ajaxSubmit(options); 
        }
        return;
    }
    var ActivityGuid = $("#hActivityGuid").val();
    var ProcessCheckState = $('input[name="CheckState"]:checked').val();
    GetProcessCheckUser(ActivityGuid, ProcessCheckState, function (selectPostList) {
        if (selectPostList != null || selectPostList != undefined) {
            var arr = new Array();
            $.each(selectPostList.UserIDList, function (i, item) {
                var obj = new Object();
                obj.Key = item.UserID;
                obj.Value = item.Name;
                arr.push(obj);
            });
            selectPostList.UserIDList = arr; 
        }
        var checkUserList = selectPostList;
        if (checkUserList == null) return;
        if (checkUserList.IsComplete == false) {
            if (checkUserList.UserIDList == null || checkUserList.UserIDList.length == 0) {
                alert("你还没选择相关审核人，请选择！");
                return;
            }
        }
        var FormConditionValue = JSON.stringify(GetFormCondition());
        if ($(".curentContainer").length == 0) {
            var options = {
                data: {
                    FormConditionValue: FormConditionValue, CheckLogUser: JSON.stringify(checkUserList.UserIDList),
                    NextActivityGuid: checkUserList.ActivityGuid
                },
                success: function (data) {
                    if (checkUserList.CustomeFunction != null && checkUserList.CustomeFunction != "") {
                        call(checkUserList.CustomeFunction);
                    }
                    alert("表单审核成功");
                    winClose(1);
                }
            };
            $("#checkfrm").ajaxSubmit(options);
            return;
        }
        var FormCheckModel = new Object();
        FormCheckModel.CheckContent = "";
        FormCheckModel.CheckState = $("#txtCheckState").val();
        FormCheckModel.SendUserID = $("#checkSendUserID").val();
        FormCheckModel.CreateTime = $("#checkCreateTime").val();
        FormCheckModel.AppInstanceName = $("#hAppInstanceName").val();
        FormCheckModel.SendUserName = $("#checkSendUserName").val();
        FormCheckModel.ProcessGuid = $("#hProcessGuid").val();
        FormCheckModel.AppInstanceID = $("#hAppInstanceID").val();
        FormCheckModel.ActivityGuid = $("#hActivityGuid").val();
        var options = {
            data: {
                FormConditionValue: FormConditionValue, CheckLogUser: JSON.stringify(checkUserList.UserIDList),
                NextActivityGuid: checkUserList.ActivityGuid, strFormCheckModel: JSON.stringify(FormCheckModel)
            },
            success: function (data) {
                alert("表单审核成功");
                winClose(1);
            }
        };
        $(".curentContainer form").ajaxSubmit(options);
    });  
}

function Print() {
    $(".PrintArea").printArea();
}
function ViewProcess() {

}

function Revoke() {
}
function Consulation() {
    var ProcessGUID = $("#hidediv input[name='ProcessGuid']").val();
    var AppInstanceID = $("#hAppInstanceID").val();
    var formUrl = '../Form/UCConsulation?ProcessGUID=' + ProcessGUID + "&&AppInstanceID=" + AppInstanceID;
    window.top.oDG1 = $.dialog({
        lock: true,
        background: '#FFF',
        opacity: 0.2,
        id: 'esDialog',
        title: "征求意见",
        titleAlign: 'left',
        content: "url:" + formUrl,
        width: 400,
        height: 300,
        btnBar: false,
        lockScroll: true
    });

}
function Circulate() {
    var url = '../Employee/EmployeeSelect';
    var formUrl = '../Form/SendFormCirculated';
    OpenSelectUserPop(url, function (selectPostList) {
        if (selectPostList != null || selectPostList != undefined) {
            var arr = new Array();
            $.each(selectPostList, function (i, item) {
                var obj = new Object();
                obj.Key = item.UserID;
                obj.Value = item.Name;
                arr.push(obj);
            });
            var ProcessGUID = $("#hidediv input[name='ProcessGuid']").val();
            var AppInstanceID = $("#hAppInstanceID").val();
            $.post(formUrl, { ProcessGUID: ProcessGUID, AppInstanceID: AppInstanceID, UserList: JSON.stringify(arr) }, function (data) {
                alert("表单传阅成功！");
            });
        }
    });
}
function hidden() {
    $(".f_btn").hide();
    $(".right_con_f").hide(); 
    $(".from_r").hide(); 
}

function setHeight() {
    $(".from_r").height($(".from_l").height() + 50);
    $(".right_con_f").height($(".from_l").height() + 50);
}

function DeleteAgreement(formID)
{
    var url = '../word/DeleteAgreement?formID=' + formID;
    $.post(url, function (data) {
        alert("删除协议成功！");
    });
}

function DeleteFall(formID) {
    var url = '../word/DeleteFall?formID=' + formID;
    $.post(url, function (data) {
        alert("删除协议成功！");
    });
}

function OpenPop(url, titleName, width, height) {
    width = width || 650;
    height = height || 300;
    window.top.winObj = window; 
    window.top.oDG = $.dialog({
        lock: true,
        background: '#FFF',
        opacity: 0.2,
        id: 'esDialog',
        title: titleName,
        titleAlign: 'left',
        content: "url:" + url,
        width: width,
        height: height,
        btnBar: false,
        lockScroll: true
    });
}
function PrintAgreement(formID)
{
    var url = '../word/IsExistAgreement?formID=' + formID;
    $.post(url, function (data) {
        if (data == false) {
            url = "../word/SelectTempldate?templateType=0&&formID=" + formID;
            OpenPop(url, "协议模板选择", 340, 120);
        } else
        {
            url = '../ContractFolder/Agreement/' + formID + ".doc";  
            window.open(url);
        }
    });
}
function PrintFall(formID)
{
    var url = '../word/IsExistFall?formID=' + formID;
    $.post(url, function (data) {
        if (data == false) {
            url = "../word/SelectTempldate?templateType=1&&formID=" + formID;
            OpenPop(url, "保函模板选择", 340, 120);
        } else {
            url = '../ContractFolder/Fall/' + formID + ".doc";
            window.open(url);
        }
    });
   
}
 


(function ($) {

    setHeight();
})(jQuery);
